package com.smile.service.entity.response;

import com.alibaba.fastjson2.annotation.JSONField;
import com.alibaba.fastjson2.TypeReference;
import java.lang.reflect.Type;
import java.util.Map;

public class DeviceConfigResponse<T> {

    @JSONField(name = "bid")
    private String bid;

    @JSONField(name = "gateway")
    private String gateway;

    @JSONField(name = "tid")
    private String tid;

    @JSONField(name = "timestamp")
    private long timestamp;

    @JSONField(name = "method")
    private String method;

    @JSONField(name = "data")
    private T data;  // 泛型类型，支持不同类型的配置数据

    // 辅助方法：获取数据类型的TypeReference，用于反序列化
//    public static <E> TypeReference<DeviceConfigResponse<E>> getTypeReference(Class<E> dataType) {
//        return new TypeReference<DeviceConfigResponse<E>>() {
//            @Override
//            public Type getType() {
//                // 构建包含具体类型的ParameterizedType
//                return new com.alibaba.fastjson2.ParameterizedTypeImpl(
//                        new Type[]{dataType},
//                        null,
//                        DeviceConfigResponse.class
//                );
//            }
//        };
//    }

    // 通用配置类 - 用于存储未知结构的数据
    public static class GenericConfig {
        private Map<String, Object> fields;

        public GenericConfig(Map<String, Object> fields) {
            this.fields = fields;
        }

        public Object get(String key) {
            return fields.get(key);
        }

        public String getString(String key) {
            Object value = fields.get(key);
            return value != null ? value.toString() : null;
        }

        public Integer getInteger(String key) {
            Object value = fields.get(key);
            return value != null ? ((Number) value).intValue() : null;
        }

        public Long getLong(String key) {
            Object value = fields.get(key);
            return value != null ? ((Number) value).longValue() : null;
        }

        @Override
        public String toString() {
            return fields != null ? fields.toString() : "{}";
        }
    }

    // Getters & Setters
    public String getBid() {
        return bid;
    }

    public void setBid(String bid) {
        this.bid = bid;
    }

    public String getGateway() {
        return gateway;
    }

    public void setGateway(String gateway) {
        this.gateway = gateway;
    }

    public String getTid() {
        return tid;
    }

    public void setTid(String tid) {
        this.tid = tid;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    // 静态实用方法 - 获取配置的值（动态方式）
    @SuppressWarnings("unchecked")
    public static <E> E getConfigValue(Object config, String key, Class<E> type) {
        if (config instanceof Map) {
            Map<String, Object> configMap = (Map<String, Object>) config;
            Object value = configMap.get(key);
            if (value != null && type.isInstance(value)) {
                return type.cast(value);
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return "DeviceConfigResponse{" +
                "bid='" + bid + '\'' +
                ", gateway='" + gateway + '\'' +
                ", tid='" + tid + '\'' +
                ", timestamp=" + timestamp +
                ", method='" + method + '\'' +
                ", data=" + data +
                '}';
    }
}